﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using IFRS.DataAccess.DAL;
using IFRS.Web;

public partial class Index_Index : PageBase
{

    #region "---------- Property -----------"

    private TREEVIEW _TREEVIEW = null;
    private TREEVIEW TREEVIEW
    {
        get
        {
            if (_TREEVIEW == null)
            {
                _TREEVIEW = new TREEVIEW();
            }

            return _TREEVIEW;
        }
    }

    #endregion

    #region "---------- Events -------------"

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PopulateMainMenu();
            //TreeViewMenu.CollapseAll();
            TreeViewMenu.ExpandAll();
        }
    }

    protected void TreeViewMenu_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            switch (e.Node.Depth)
            {
                case 0:
                    PopulateSubMainMenu(Int32.Parse(e.Node.Value), e.Node);
                    break;
                case 1:
                    PopulateMenu(Int32.Parse(e.Node.Value), e.Node);
                    break;
            }
        }
    }

    #endregion

    #region "---------- Methods ------------"

    private void PopulateMainMenu()
    {
        DataTable dt = new DataTable();
        if (SessionManager.IFRS_ROLE_ID == -1)
        {
            dt = TREEVIEW.MAINMENU_SEARCH_ALL();
        }
        else
        {
            dt = TREEVIEW.MAINMENU_USER_SEARCH_ALL(SessionManager.IFRS_ROLE_ID);
        }

        PopulateNodesMainMenu(dt, TreeViewMenu.Nodes);
    }

    private void PopulateSubMainMenu(int iParentID, TreeNode tParentNode)
    {
        DataTable dt = new DataTable();
        if (SessionManager.IFRS_ROLE_ID == -1)
        {
            dt = TREEVIEW.SUBMAINMENU_SEARCH_BYID(iParentID);
        }
        else
        {
            dt = TREEVIEW.SUBMAINMENU_USER_SEARCH_BYID(SessionManager.IFRS_ROLE_ID,iParentID);
        }

        PopulateNodesSubMainMenu(dt, tParentNode.ChildNodes);
    }

    private void PopulateMenu(int iParentID, TreeNode tParentNode)
    {
        DataTable dt = new DataTable();
        if (SessionManager.IFRS_ROLE_ID == -1)
        {
            dt = TREEVIEW.MENU_SEARCH_BYID(iParentID);
        }
        else
        {
            dt = TREEVIEW.MENU_USER_SEARCH_BYID(SessionManager.IFRS_ROLE_ID,iParentID);
        }

        PopulateNodesMenu(dt, tParentNode.ChildNodes);
    }

    private void PopulateNodesMainMenu(DataTable dt, TreeNodeCollection nodes)
    {
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            i = i + 1;
            TreeNode tn = new TreeNode();
            tn.Text = dr["MENUCATEGORYNAME"].ToString();
            tn.Value = dr["MENUCATEGORYID"].ToString();
            nodes.Add(tn);
            tn.PopulateOnDemand = true;
        }
    }

    private void PopulateNodesSubMainMenu(DataTable dt, TreeNodeCollection nodes)
    {
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            i = i + 1;
            TreeNode tn = new TreeNode();
            tn.Text = dr["MENUSUBCATEGORYNAME"].ToString();
            tn.Value = dr["MENUSUBCATEGORYID"].ToString();
            nodes.Add(tn);
            tn.PopulateOnDemand = true;
        }
    }

    private void PopulateNodesMenu(DataTable dt, TreeNodeCollection nodes)
    {
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            i = i + 1;
            TreeNode tn = new TreeNode();
            tn.Text = dr["MENUITEMNAME"].ToString();
            tn.Value = dr["MENUITEMID"].ToString();
            tn.NavigateUrl = ResolveUrl("~/" + dr["MENUURL"].ToString() + "?pid=" + dr["MENUITEMID"].ToString());
            nodes.Add(tn);
        }
    }

    #endregion
}